/**
 * @Title: detail.js
 * @Description: detail
 * @author: Fanchao
 * @date: 2020/8/30 11:40 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import storageUtils from 'utils/storage';
import { getExamDetailModel } from 'api/index';


export default {
    namespace: 'examinationTaskDetail',
    state: {
        detailData: {},
    },
    reducers: {
        setDetailData(state, { payload: detailData }) {
            return { ...state, detailData };
        },
    },
    effects: {
        /**
         * @desc 管理员获取问卷
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getExamDetailModel({ payload }, { call, put, select }) {
            const result = yield call(getExamDetailModel, payload);
            if (result.status === 'success') {
                const detailData = result.result;
                yield put({
                    type: 'setDetailData',
                    payload: detailData,
                });
            } else {
                message.error(result.message);
            }

        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                const match = pathToRegexp('/evaluations/examination/:id').exec(pathname);
                if (match) {
                    dispatch({
                        type: 'getExamDetailModel',
                        payload: {
                            accessToken: storageUtils.getToken(),
                            evaluateExamId: match[1],
                        },
                    });
                }
            });
        },
    },
};
